		TITLE	REALRELO - Copyright (c) SLR Systems 1994

		INCLUDE	MACROS
		INCLUDE	SEGMENTS
		INCLUDE	RELOCSS
		INCLUDE	EXES


		PUBLIC	REAL_RELOC_FLUSH,PACK_MOVE_FINAL_HIGH,REAL_RELOC_F1_NOPACK


		.DATA

		EXTERNDEF	TEMP_RECORD:BYTE

		EXTERNDEF	RELOC_COUNT:DWORD,RELOC_CHUNK_COUNT:DWORD,TOTAL_RELOCS:DWORD,CURN_SECTION_GINDEX:DWORD
		EXTERNDEF	RELOC_BITS_LOG:DWORD,LIDATA_RELOCS_NEXT_LOG:DWORD,RELOC_HIGH_WATER:DWORD,RELOC_BITS:DWORD
		EXTERNDEF	LIDATA_RELOCS_NEXT:DWORD,FINAL_HIGH_WATER:DWORD,NEW_REPT_ADDR:DWORD,FIX2_EXEPACK_BASE:DWORD
		EXTERNDEF	FIX2_SM_START:DWORD,FIRST_RELOC_GINDEX:DWORD,FIRST_SECTION_GINDEX:DWORD,LAST_RELOC_GINDEX:DWORD

		EXTERNDEF	RELOC_GARRAY:STD_PTR_S,EXEHEADER:EXE,RELOC_STUFF:ALLOCS_STRUCT,REAL_RELOC_STRUCT:SEQ_STRUCT


		.CODE	PASS2_TEXT

		EXTERNDEF	_release_minidata:proc,RELEASE_BLOCK:PROC,MOVE_EAX_TO_FINAL_HIGH_WATER:PROC,MOVE_PACK_DATA:PROC
		EXTERNDEF	STORE_EAXECX_EDX_SEQ:PROC,SET_EXEPACK_STUFF:PROC,ERR_ABORT:PROC,MOVE_EXEHDR_TO_FINAL:PROC
		EXTERNDEF	RELEASE_SEGMENT:PROC,RELEASE_GARRAY:PROC

		EXTERNDEF	CANNOT_PACK_ERR:ABS


REAL_RELOC_FLUSH	PROC
		;
		;
		;
		GETT	AL,PACKING_RELOCS
		GETT	DL,CHAINING_RELOCS

		OR	AL,DL
		JNZ	L5$

		MOV	EAX,RELOC_COUNT		;# OF RELOCS THIS SEGMENT
		MOV	ECX,FIX2_EXEPACK_BASE

		TEST	EAX,EAX
		JZ	L9$			;NO RELOCS, QUIT

		SHR	ECX,4
		PUSH	EDI

		ADD	EXEHEADER._EXE_N_RELOC,AX
		MOV	EDX,FIRST_RELOC_GINDEX

		SHL	ECX,16
		MOV	EDI,OFF TEMP_RECORD
L1$:
		CONVERT	EDX,EDX,RELOC_GARRAY
		ASSUME	EDX:PTR RELOC_STRUCT

		MOV	EAX,[EDX]._RL_SEG_OFFSET
		MOV	EDX,[EDX]._RL_NEXT_RELOC_GINDEX

		CMP	EAX,64K
		JAE	L4$

		OR	EAX,ECX
L41$:
		MOV	[EDI],EAX
		ADD	EDI,4

		CMP	EDI,OFF TEMP_RECORD+TEMP_SIZE-10	;FLUSH THIS STUFF 1K BLOCKS
		JAE	L3$
L21$:
		TEST	EDX,EDX
		JNZ	L1$

		CALL	REAL_RELOC_F1_NOPACK	;FLUSH TEMP_RECORD

		POP	EDI
		JMP	L89$

L9$:
		RET

L4$:
		PUSH	EBX
		MOV	EBX,EAX

		SHR	EBX,4
		AND	EAX,4

		SHL	EBX,16
		OR	EAX,ECX

		ADD	EAX,EBX
		POP	EBX

		JMP	L41$

L3$:
		PUSHM	EDX,ECX

		CALL	REAL_RELOC_F1_NOPACK

		POPM	ECX,EDX

		JMP	L21$

L5$:
		MOV	EAX,RELOC_COUNT		;# OF RELOCS THIS SEGMENT
		MOV	ECX,FIX2_EXEPACK_BASE

		TEST	EAX,EAX
		JZ	L65$			;NO RELOCS, QUIT

		SHR	ECX,4
		PUSH	EDI

		ADD	TOTAL_RELOCS,EAX
		MOV	EDX,FIRST_SECTION_GINDEX

		SHL	ECX,16
		MOV	EDI,OFF TEMP_RECORD

		INC	RELOC_CHUNK_COUNT	;# OF RELOC CHUNKS THIS SECTION

		CMP	EAX,8K-3 		;CANNOT HAVE MORE THAN THIS...
		JA	L8$

		CMP	CURN_SECTION_GINDEX,EDX
		JNZ	L501$

		MOV	DPTR [EDI],-1		;HERE COMES RELOCATIONS
		ADD	EDI,2

L501$:
		OR	EAX,ECX			;# RELOCS AND THEIR FRAME
		MOV	EDX,FIRST_RELOC_GINDEX

		MOV	[EDI],EAX
		ADD	EDI,4

L51$:
		CONVERT	EDX,EDX,RELOC_GARRAY
		ASSUME	EDX:PTR RELOC_STRUCT

		MOV	EAX,[EDX]._RL_SEG_OFFSET
		MOV	ECX,[EDX]._RL_OS2_NUMBER	;REFERENCED PARAGRAPH

		MOV	WPTR [EDI],AX
		MOV	EDX,[EDX]._RL_NEXT_RELOC_GINDEX

		MOV	WPTR [EDI+2],CX
		ADD	EDI,4

		CMP	EDI,OFF TEMP_RECORD+1K	;FLUSH THIS STUFF 1K BLOCKS
		JAE	L7$
L61$:
		TEST	EDX,EDX
		JNZ	L51$

		CALL	REAL_RELOC_F1_PACK	;FLUSH TEMP_RECORD

		POP	EDI
L65$:
		;
		;SET UP REAL_EXEPACK STUFF
		;
		MOV	EAX,FINAL_HIGH_WATER
if	fg_slrpack
		GETT	CL,SLRPACK_FLAG

		OR	CL,CL
		JZ	L67$

		MOV	EAX,NEW_REPT_ADDR
L67$:
endif

		CALL	SET_EXEPACK_STUFF
		;
		MOV	EAX,FIX2_SM_START
		MOV	ECX,RELOC_COUNT

		AND	AL,0F0H
		TEST	ECX,ECX

		MOV	FIX2_EXEPACK_BASE,EAX
		JZ	L899$
L89$:
		;
		;RELEASE RELOC SPACE
		;
		MOV	EAX,OFF RELOC_STUFF
		push	EAX
		call	_release_minidata
		add	ESP,4

		MOV	EAX,OFF RELOC_GARRAY
		CALL	RELEASE_GARRAY

		XOR	EAX,EAX

		MOV	RELOC_COUNT,EAX
		MOV	RELOC_HIGH_WATER,EAX

		MOV	FIRST_RELOC_GINDEX,EAX
		MOV	LAST_RELOC_GINDEX,EAX

		XCHG	EAX,RELOC_BITS
		CALL	RELEASE_BLOCK

if	page_size EQ 8K

		XOR	EAX,EAX
		XCHG	EAX,LIDATA_RELOCS_START
		OR	EAX,EAX
		JZ	L895$
		CALL	RELEASE_BLOCK
L895$:
endif

		XOR	EAX,EAX
		MOV	LIDATA_RELOCS_NEXT,EAX
L899$:
		RET

L8$:
		MOV	AX,CANNOT_PACK_ERR
		CALL	ERR_ABORT

L7$:
		PUSH	EDX
		CALL	REAL_RELOC_F1_PACK

		POP	EDX
		JMP	L61$

REAL_RELOC_FLUSH	ENDP


REAL_RELOC_F1_NOPACK	PROC
		;
		;
		;
		MOV	ECX,EDI
		MOV	EDI,OFF TEMP_RECORD

		SUB	ECX,EDI
		JZ	L9$

		MOV	EAX,EDI
		JMP	MOVE_EXEHDR_TO_FINAL

L9$:
		RET

REAL_RELOC_F1_NOPACK	ENDP


REAL_RELOC_F1_PACK	PROC
		;
		;
		;
		MOV	ECX,EDI
		MOV	EDI,OFF TEMP_RECORD

		SUB	ECX,EDI
		JZ	L9$

		MOV	EAX,FIRST_SECTION_GINDEX
		MOV	EDX,CURN_SECTION_GINDEX

		CMP	EAX,EDX
		JNZ	L5$

		MOV	EAX,EDI
		JMP	PACK_MOVE_FINAL_HIGH

L9$:
		RET

L5$:
		MOV	EAX,EDI
		MOV	EDX,OFF REAL_RELOC_STRUCT

		JMP	STORE_EAXECX_EDX_SEQ

REAL_RELOC_F1_PACK	ENDP


PACK_MOVE_FINAL_HIGH	PROC

if	fg_slrpack
		GETT	DL,SLRPACK_FLAG

		OR	DL,DL
		JZ	MOVE_EAX_TO_FINAL_HIGH_WATER

		MOV	EDX,NEW_REPT_ADDR

		ADD	NEW_REPT_ADDR,ECX
		JMP	MOVE_PACK_DATA

else
		JMP	MOVE_EAX_TO_FINAL_HIGH_WATER
endif

PACK_MOVE_FINAL_HIGH	ENDP


		END

